﻿using System.Collections.Generic;
using UnityEngine;

namespace NpcFramework
{
    public class NpcSeekerManager : MonoBehaviour
    {

        [SerializeField]
        private List<NpcSeeker> _seekers;

        private Dictionary<int, NpcSeeker> _seekerMap;
        
        public void OnInit()
        {
            _seekers = new List<NpcSeeker>();
            _seekerMap = new Dictionary<int, NpcSeeker>();
        }

        public void AddNpcSeeker(int id,NpcSeeker npcSeeker)
        {
            if (_seekerMap.ContainsKey(id))
            {
                UnityEngine.Debug.LogError("重复添加Seeker");
                return;
            }
            
            npcSeeker.ID = id;
            _seekers.Add(npcSeeker);
            _seekerMap[id] = npcSeeker;
            npcSeeker.transform.parent = this.transform;
            npcSeeker.name = string.Format("{0}_Seeker", id);
        }

        public void RemoveNpcSeeker(int id, bool destroy = true)
        {
            int sIndex = GetSeekerIndex(id);
            if (sIndex == ConstValue.INVALID_INDEX)
            {
                return;
            }

            _seekerMap.Remove(id);
            if (destroy)
            {
                NpcSeeker seeker = _seekers[sIndex];
                _seekers.RemoveAt(sIndex);
                //GameObject.Destroy(seeker);
                if (seeker != null)
                {
                    GameObject.DestroyImmediate(seeker.gameObject);
                }
            }
            else
            {
                _seekers.RemoveAt(sIndex);
            }
        }

        public List<NpcSeeker> GetSeekers()
        {
            return _seekers;
        }

        public NpcSeeker GetNpcSeeker(int npcId)
        {
            NpcSeeker npcSeeker;
            if (_seekerMap.TryGetValue(npcId, out npcSeeker))
            {
                return npcSeeker;
            }

            return null;
        }

        public void SetNpcDestination(int id, Vector3 destination)
        {
            NpcSeeker npcSeeker;
            if (_seekerMap.TryGetValue(id,out npcSeeker))
            {
                npcSeeker.SetDestination(destination);
            }
        }
        
        private int GetSeekerIndex(int id)
        {
            for (int i = 0; i < _seekers.Count; i++)
            {
                if (_seekers[i].ID == id)
                {
                    return i;
                }
            }

            return ConstValue.INVALID_INDEX;
        }

        public void Dispose()
        {
            _seekerMap?.Clear();
            _seekerMap = null;

            if (_seekers!=null)
            {
                for (int i = _seekers.Count-1; i >=0; i--)
                {
                    var seeker = _seekers[i];
                    if (seeker != null)
                    {
                        seeker.Dispose();
                        GameObject.DestroyImmediate(seeker.gameObject);
                    }
                }
            }

        }
    }

}
